package com.cin.cma.server.pos.config;

import com.alibaba.fastjson2.JSON;
import com.cin.cma.common.constant.EventConstant;
import com.cin.cma.common.constant.MsgTypeConstant;
import com.cin.cma.common.enums.JyServerErrorCode;
import com.cin.cma.common.exception.UserFriendlyException;
import com.cin.cma.common.model.SocketRequest;
import com.cin.cma.common.model.SocketResponse;
import com.cin.cma.server.pos.model.socket.SocketBody;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 *
 * @Date：2024-07-16 18:14
 */
@Component
public class SocketIOService {
    public static Map<String, SocketIOClient> clientMap = new HashMap<>();
    public static Map<String, SocketBody> response = new ConcurrentHashMap<>(100000);
    public static SocketIOServer socketIOServer;

    //给容器内所有的客户端发送信息
    public void sendMessage(SocketRequest socketRequest, String cinemaLinkId, String event) throws Exception {
        if (clientMap.isEmpty() || !clientMap.containsKey(cinemaLinkId)) {
            throw new UserFriendlyException(JyServerErrorCode.CLIENT_OFFLINE);
        }
        //请求消息类型为异步时不需要装载结果，如果为同步时需要装载结果到MAP等外层取出。
        if (!socketRequest.getMessageType().equals(MsgTypeConstant.ASYNC)) {
            response.put(socketRequest.getRequestId(), new SocketBody(socketRequest));
        }
        clientMap.get(cinemaLinkId).sendEvent(event, socketRequest);
    }

    /**
     * 设置客户端返回消息
     *
     * @param data 数据体
     */
    private void setResponse(String data) {
        SocketResponse socketResponse = JSON.parseObject(data, SocketResponse.class);
        response.get(socketResponse.getRequestId()).setResposne(socketResponse);
        System.out.println("服务器接收到消息： " + socketResponse);
    }

    @Bean
    @DependsOn("socketIOServer")
    public void listener() {
        socketIOServer.addEventListener(EventConstant.Other.OTHER_CUSTOM, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Basic.BASIC_GET_PASS_NUMBER, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Basic.BASIC_GET_LOCAL_CINEMA, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Basic.BASIC_BOOKING_ORDER, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Basic.BASIC_GLOBAL_SYSTEM_TIME, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Basic.BASIC_GET_INCREASE_BOOKING_SETTING, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Basic.BASIC_ALL_SYSTEM_SETTING, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Card.CARD_CONSUME, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Card.CARD_GET_CARD_INFO, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Card.CARD_CONSUME_ROLLBACK, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Card.CARD_SUMMARY_INFO, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Card.CARD_ADD_MONEY, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Card.CARD_BUYING, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Card.CARD_GET_ADD_MONEY, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Card.CARD_GET_MARKING, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Card.CARD_RESET_PWD, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Card.CARD_LOCK, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Card.CARD_ACTIVATION, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Card.CARD_EDIT_MEMBER_INFO, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Card.CARD_SELL, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Card.CARD_SELL_RECHARGE, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Card.CARD_SELL_INFO, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Card.CARD_GET_USE_POLICY_LIST, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Card.CARD_USE_POLICY, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Card.CARD_GET_GRADE_LIST, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.DataInf.DATAINF_GET_SOFTWARE_VERSION, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Ticket.TICKET_GET_SESSION, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Ticket.TICKET_GET_SESSION_FILMS, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Ticket.TICKET_GET_HALLS, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Ticket.TICKET_GET_SECTIONS, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Ticket.TICKET_GET_SEAT_PLAN, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Ticket.TICKET_GET_SESSION_PRICE_POLICY, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Ticket.TICKET_GET_BOOKING_ID, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Ticket.TICKET_REPOSITION_SEATS, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Ticket.TICKET_RELEASE_SEATS, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Ticket.TICKET_GET_ALLOCATE_SEATS, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Ticket.TICKET_GET_CONFIRM_ID, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Ticket.TICKET_QUERY_ORDER, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Ticket.TICKET_GET_BOOKING_BILL, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Ticket.TICKET_GET_TICKETS, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Ticket.TICKET_REFUND, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Ticket.TICKET_CARD_PRICE_POLICY, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Ticket.TICKET_FILM_IMAGE, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Voucher.VOUCHER_REFUND, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Voucher.VOUCHER_GET, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Voucher.VOUCHER_TEMP_LOCK, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Voucher.VOUCHER_REDEEM, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Voucher.VOUCHER_REDEEM_ROLLBACK, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Voucher.VOUCHER_GET_PAY_POLICY, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Goods.GOODS_GET_GOODS_CLASS, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Goods.GOODS_GET_GOODS_HOT, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Goods.GOODS_GET_GOODS_LIST, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Goods.GOODS_GET_GOODS_PACKAGE, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Goods.GOODS_GET_PACKAGE_DETAILS, String.class, (s, data, v) -> this.setResponse(data));
        socketIOServer.addEventListener(EventConstant.Goods.GOODS_GET_ORDER_INFO, String.class, (s, data, v) -> this.setResponse(data));
    }
}
